package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.CGSystemPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.ICGSystemView;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.CreateGroupActivity;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/1/25  Time:下午 1:21.
 * description: 创建系统随机分组 .
 */
public class CGSystemFargment extends BaseMvpFragment<ICGSystemView, CGSystemPresenter> implements  ICGSystemView,View.OnClickListener{
    @BindView(R.id.tv_g_num)
    TextView tv_g_num;

    @BindView(R.id.stv_tab1)
    SuperTextView stv_tab1;
    @BindView(R.id.stv_tab2)
    SuperTextView stv_tab2;
    private int groupNum;
    private int userNum;
    private int type;
    public static CGSystemFargment getInstance(){
        return new CGSystemFargment();
    }
    @Override
    protected CGSystemPresenter createPresenter() {
        return new CGSystemPresenter();
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return dialog;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    public int getLayout() {
        return R.layout.frag_cg_system;
    }

    @Override
    public void initView() {
        stv_tab1.setSelected(true);
        stv_tab1.setOnClickListener(tabClick);
        stv_tab2.setSelected(false);
        stv_tab2.setOnClickListener(tabClick);
        findViewById(R.id.iv_g_sum).setOnClickListener(this::onClick);
        findViewById(R.id.iv_g_add).setOnClickListener(this::onClick);
        findViewById(R.id.stv_submit).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int num=type==0?groupNum:userNum;
        switch (view.getId()){
            case R.id.iv_g_sum:
                if(num==0){
                    return;
                }
                num-=1;
                tv_g_num.setText(String.valueOf(num));
                if(type==0){
                    groupNum=num;
                }else {
                    userNum=num;
                }
                break;
            case R.id.iv_g_add:
                num+=1;
                tv_g_num.setText(String.valueOf(num));
                if(type==0){
                    groupNum=num;
                }else {
                    userNum=num;
                }
                break;
            case R.id.stv_submit:
                mPresenter.submit();
                break;
        }
    }

    private View.OnClickListener tabClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view == stv_tab1) {
                if (stv_tab1.isSelected()) {
                    return;
                }
                setTabSel(stv_tab1, true);
                setTabSel(stv_tab2, false);
                setType(0);
            } else if (view == stv_tab2) {
                if (stv_tab2.isSelected()) {
                    return;
                }
                setTabSel(stv_tab1, false);
                setTabSel(stv_tab2, true);
                setType(1);
            }
        }
    };
    private void setType(int t){
        type=t;
        tv_g_num.setText(String.valueOf(type==0?groupNum:userNum));
    }
    private void setTabSel(SuperTextView tab, boolean isSel) {
        tab.setSelected(isSel);
        tab.setShaderEnable(isSel);
        tab.setTextColor(isSel ? 0xffffffff : 0xff344854);
    }
    @Override
    public void loadEnd() {
        CreateGroupActivity.start(4);
        getActivity().finish();
    }

    @Override
    public void load() {
        ViewGroup ll_load=findViewById(R.id.ll_load);
        ll_load.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }
}
